<template>
  <el-container class="panel-block">
    <el-header class="sa-header">
      <div class="sa-title sa-flex sa-row-between">
        <div class="label sa-flex">
          <span class="left">富文本</span>
          <search-condition
            :conditionLabel="filterParams.conditionLabel"
            @deleteFilter="deleteFilter"
          ></search-condition>
        </div>
        <div>
          <el-button class="sa-button-refresh" icon="RefreshRight" @click="getData()"></el-button>
          <el-button class="sa-button-refresh" icon="Search" @click="openFilter"></el-button>
          <el-button
            v-auth="'shop.admin.data.richtext.add'"
            icon="Plus"
            type="primary"
            @click="addRow"
            >新建</el-button
          >
        </div>
      </div>
    </el-header>
    <el-main class="sa-p-0" v-loading="loading">
      <el-table
        height="100%"
        class="sa-table"
        :data="table.data"
        @selection-change="changeSelection"
        @sort-change="fieldFilter"
        @row-dblclick="editRow"
        row-key="id"
        stripe
      >
        <template #empty>
          <sa-empty />
        </template>
        <el-table-column type="selection" width="48" align="center"></el-table-column>
        <el-table-column prop="id" label="ID" min-width="100" sortable="custom"> </el-table-column>
        <el-table-column label="标题" min-width="100">
          <template #default="scope">
            <span class="sa-table-line-1">
              {{ scope.row.title || '-' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" min-width="160">
          <template #default="scope">
            {{ scope.row.create_time || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="更新时间" min-width="160">
          <template #default="scope">
            {{ scope.row.update_time || '-' }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" min-width="120">
          <template #default="scope">
            <el-button
              v-auth="'shop.admin.data.richtext.detail'"
              type="primary"
              class="is-link"
              @click="editRow(scope.row)"
              >编辑</el-button
            >
            <el-popconfirm
              width="fit-content"
              confirm-button-text="确认"
              cancel-button-text="取消"
              title="确认删除这条记录?"
              @confirm="deleteApi(scope.row.id)"
            >
              <template #reference>
                <el-button v-auth="'shop.admin.data.richtext.delete'" type="danger" class="is-link">
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <sa-view-bar>
      <template #left>
        <sa-batch-handle
          :batchHandleTools="batchHandleTools"
          :selectedLeng="table.selected.length"
          @batchHandle="batchHandle"
        ></sa-batch-handle>
      </template>
      <template #right>
        <sa-pagination :pageData="pageData" @updateFn="getData" />
      </template>
    </sa-view-bar>
  </el-container>
</template>
<script setup>
  import { onMounted, reactive, ref } from 'vue';
  import { api } from '../data.service';
  import { ElMessageBox } from 'element-plus';
  import { useModal, usePagination } from '@/sheep/hooks';
  import { useSearch } from '@/sheep/components/sa-table/sa-search/useSearch';
  import { composeFilter } from '@/sheep/utils';
  import RichtextEdit from './edit.vue';
  import { cloneDeep } from 'lodash';

  const { pageData } = usePagination();
  const filterParams = reactive({
    tools: {
      keyword: {
        type: 'tinput',
        field: 'keyword',
        value: '',
        label: '搜索内容',
        placeholder: '请输入查询内容',
      },
    },
    data: {
      keyword: '',
    },
    conditionLabel: {},
  });
  const { openFilter, deleteFilter } = useSearch({ filterParams, getData });
  // 列表
  const table = reactive({
    data: [],
    order: '',
    sort: '',
    selected: [],
  });
  const loading = ref(true);
  // 获取
  async function getData(page) {
    loading.value = true;
    if (page) pageData.page = page;
    let tempSearch = cloneDeep(filterParams.data);
    let search = composeFilter(tempSearch);
    const { error, data } = await api.richtext.list({
      page: pageData.page,
      list_rows: pageData.list_rows,
      order: table.order,
      sort: table.sort,
      ...search,
    });
    if (error === 0) {
      table.data = data.data;
      pageData.page = data.current_page;
      pageData.list_rows = data.per_page;
      pageData.total = data.total;
    }
    loading.value = false;
  }
  // table 字段排序
  function fieldFilter({ prop, order }) {
    table.order = order == 'ascending' ? 'asc' : 'desc';
    table.sort = prop;
    getData();
  }
  //table批量选择
  function changeSelection(row) {
    table.selected = row;
  }
  const batchHandleTools = [
    {
      type: 'delete',
      label: '删除',
      auth: 'shop.admin.data.richtext.delete',
      class: 'danger',
    },
  ];
  function addRow() {
    useModal(
      RichtextEdit,
      { title: '新建', type: 'add' },
      {
        confirm: () => {
          getData();
        },
      },
    );
  }
  function editRow(row) {
    useModal(
      RichtextEdit,
      {
        title: '编辑',
        type: 'edit',
        id: row.id,
      },
      {
        confirm: () => {
          getData();
        },
      },
    );
  }
  // 删除api 单独批量可以直接调用
  async function deleteApi(id) {
    await api.richtext.delete(id);
    getData();
  }
  async function batchHandle(type) {
    let ids = [];
    table.selected.forEach((row) => {
      ids.push(row.id);
    });
    switch (type) {
      case 'delete':
        ElMessageBox.confirm('此操作将删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          deleteApi(ids.join(','));
        });
        break;
      default:
        await api.richtext.edit(ids.join(','), {
          status: type,
        });
        getData();
    }
  }

  onMounted(() => {
    getData();
  });
</script>
